package com.bd.scm.module.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.bd.scm.domain.models.note.Note;

/**
 * 短信发送工具类型
 *
 * @author YH
 * @version 1.0
 * @date 2020/9/22 16:27
 */
public class SmsUtils {

    //    @Value("${bd.note.accessID}")
    private static String accessID = "LTAI4G2s7CPotr7FJyzpCYbJ";

    //    @Value("${bd.note.accesskey}")
    private static String accesskey = "yP7vkBY4QyFGpz4WRW3LUf7a4cHAom";

    /**
     * 员工邀约，员工填写申请单
     * 短信模板 ${visitor}，您好，#{employee}邀请您${begin}至${end}到访舍弗勒${base}基地，请准时到达
     *
     * @param jsonObject
     */
    public static void staffFilloutSendNote(JSONObject jsonObject) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessID, accesskey);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(com.aliyuncs.http.MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//固定
        request.setSysVersion("2017-05-25");//固定
        request.setSysAction("SendSms");//固定
        request.putQueryParameter("PhoneNumbers", jsonObject.getString("visitTel"));//接收人
        request.putQueryParameter("SignName", "舍弗勒投资中国有限公司");//固定
        request.putQueryParameter("TemplateCode",
                jsonObject.getInteger("isZh") == 0 ? "SMS_205135200" : "SMS_202818744");//英文模板:中文模板

        //${visitor}，您好，#{employee}邀请您${begin}至${end}到访舍弗勒${base}基地，请准时到达
        Note note = new Note();
        note.setVisitor(jsonObject.getString("visitName"));
        note.setEmployee(jsonObject.getString("employeeName"));
        note.setBegin(jsonObject.getString("visitDateBegin"));
        note.setEnd(jsonObject.getString("visitDateEnd"));
        note.setBase(jsonObject.getString("visitBaseName"));

        String s = JSON.toJSONString(note);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(note));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 员工邀约，访客填写申请单
     * 短信模板 ${visitor}，您好，${employee}请您到访舍弗勒访问，请填写访客申请表，http://dss.schaefflercn.com:9060/appointmentLess
     *
     * @param note
     */
    public static void visitorFilloutSendNote(Note note) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessID, accesskey);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(com.aliyuncs.http.MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//固定
        request.setSysVersion("2017-05-25");//固定
        request.setSysAction("SendSms");//固定
        request.putQueryParameter("PhoneNumbers", note.getVisitTel());//接收人
        request.putQueryParameter("SignName", "舍弗勒投资中国有限公司");//固定
        request.putQueryParameter("TemplateCode",
                note.getIsZh() == 0 ? "SMS_205135204" : "SMS_204752027");//英文模板:中文模板

        //${visitor}，您好，${employee}邀请您到访舍弗勒，请填写访客申请表，http://dss.schaefflercn.com:9060/appointmentLess

        String s = JSON.toJSONString(note);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(note));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 3)访客申请被拒绝消息
     * 模板内容：${visitor}，您好，抱歉的通知您，您申请的${begin}至${end}到访舍弗勒${base}基地被拒绝，原因：${reason}
     *
     * @param jsonObject
     */
    public static void visitorRefuseSendNote(JSONObject jsonObject) {
        DefaultProfile profile = DefaultProfile.getProfile("default", accessID, accesskey);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(com.aliyuncs.http.MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//固定
        request.setSysVersion("2017-05-25");//固定
        request.setSysAction("SendSms");//固定
        request.putQueryParameter("PhoneNumbers", jsonObject.getString("visitTel"));//接收人
        request.putQueryParameter("SignName", "舍弗勒投资中国有限公司");//固定
        request.putQueryParameter("TemplateCode",
                jsonObject.getInteger("isZh") == 0 ? "SMS_205125203" : "SMS_202813684");//英文模板:中文模板


        //${visitor}，您好，抱歉的通知您，您申请的${begin}至${end}到访舍弗勒${base}基地被拒绝，原因：${reason}
        Note note = new Note();
        note.setVisitor(jsonObject.getString("visitName"));
        note.setBegin(jsonObject.getString("visitDateBegin"));
        note.setEnd(jsonObject.getString("visitDateEnd"));
        note.setBase(jsonObject.getString("visitBaseName"));
        note.setReason(jsonObject.getString("reason"));

        String s = JSON.toJSONString(note);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(note));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送短信验证码
     *
     * @param telephone 接收手机号
     * @param code      验证码
     * @throws ClientException
     */
    public static void sendSmsVerificationCode(String telephone, String code, int isZh) throws ClientException {

        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        // 初始化acsClient,暂不支持region化
        DefaultProfile profile = DefaultProfile.getProfile("default", accessID, accesskey);
        IAcsClient client = new DefaultAcsClient(profile);

        // 组装请求对象-具体描述见控制台-文档部分内容
        CommonRequest request = new CommonRequest();
        request.setSysMethod(com.aliyuncs.http.MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//固定
        request.setSysVersion("2017-05-25");//固定
        request.setSysAction("SendSms");//固定
        request.putQueryParameter("PhoneNumbers", telephone);//接收人
        request.putQueryParameter("SignName", "舍弗勒投资中国有限公司");//固定
        request.putQueryParameter("TemplateCode",
                isZh == 0 ? "SMS_205120226" : "SMS_204127521");//英文模板:中文模板

        Note note = new Note();
        note.setCode(code);
        String s = JSON.toJSONString(note);
        request.putQueryParameter("TemplateParam", JSON.toJSONString(note));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    //随机生成验证码
    public static int getNewcode() {
        int newcode = (int) (Math.random() * 999999) + 100;  //每次调用生成一次六位数的随机数
        return newcode;
    }
}
